/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Security.Permissions;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

#endregion

namespace DotNetNuke.ModuleToolkit.Controls
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The CommandLink skin object displays a Command Hyperlink
    /// </summary>
    /// <history>
    /// 	[anurse]	05/18/2006	created
    /// </history>
    /// -----------------------------------------------------------------------------
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal),
    AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
    ToolboxData("<{0}:CommandLink runat=\"server\"> </{0}:CommandLink>")]
    public class CommandLink : HyperLink
    {

        #region Private Members

        private string _ResourceKey;
        private bool _UseGlobalResources = false;

        #endregion

        #region Public Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// A String that contains the localization resource key for the Edit Mode Text
        /// </summary>
        /// <history>
        /// 	[cnurse]	04/12/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Localization"),
        DefaultValue(""),
        Description("The Resource Key to use to localize the Button Caption")]
        public string ResourceKey
        {
            get { return _ResourceKey; }
            set { _ResourceKey = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// A flag that indicates whether the Command Button icon should be displayed
        /// </summary>
        /// <history>
        /// 	[cnurse]	04/17/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Behavior"),
        DefaultValue(true),
        Description("Optionally display the Command Button icon.")]
        public bool ShowIcon
        {
            get
            {
                object b = ViewState["ShowIcon"];
                return (b == null) ? true : (bool)b;
            }
            set { ViewState["ShowIcon"] = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets whether to display the Command Button Text
        /// </summary>
        /// <history>
        /// 	[cnurse]	04/17/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Behavior"),
        DefaultValue(true),
        Description("Set whether to display the Command Button text.")]
        public bool ShowText
        {
            get
            {
                object b = ViewState["ShowText"];
                return (b == null) ? true : (bool)b;
            }
            set
            {
                ViewState["ShowText"] = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets whether to use global resources to localize the text
        /// </summary>
        /// <history>
        /// 	[anurse]	05/18/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Behavior"),
        DefaultValue(true),
        Description("Set whether to use global resources to localize the text.")]
        public bool UseGlobalResources
        {
            get
            {
                return _UseGlobalResources;
            }
            set
            {
                _UseGlobalResources = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets or sets the text to display on the button
        /// </summary>
        /// <history>
        /// 	[anurse]	05/18/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public override string Text
        {
            get
            {
                if (String.IsNullOrEmpty(ResourceKey) || DesignMode)
                    return base.Text;
                else
                    return Localization.Localization.LocalizeString(this, ResourceKey, UseGlobalResources);
            }
            set
            {
                base.Text = value;
            }
        }

        #endregion

        #region Protected Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// RenderContents renders the contents of the control
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="writer">An HtmlTextWriter</param>
        /// <history>
        /// 	[cnurse]	04/12/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (this.HasControls())
                base.RenderContents(writer);
            else
            {
                //Render Image
                if (ShowIcon)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Src, ResolveClientUrl(ImageUrl));
                    writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
                    if (ShowText)
                        writer.AddStyleAttribute(HtmlTextWriterStyle.PaddingRight, "5px");
                    writer.RenderBeginTag(HtmlTextWriterTag.Img);

                    //Render end of Image
                    writer.RenderEndTag();
                }

                //Render Hyperlink Text
                if (ShowText)
                {
                    //if (ShowIcon)
                    //    writer.Write("&nbsp;");
                    writer.Write(Text);
                }
            }
        }

        #endregion

    }
}
